﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class LoaiHangDAO
    {
        public bool ThemLoaiHang(string TenLoaiHang)
        {            
            string sql = "INSERT INTO LoaiHang(TenLoaiHang, Deleted) VALUES(N'"+TenLoaiHang+"',0)";
            using (SqlConnection conn = DataProvider.ConnectDB())            
            {
                SqlCommand cmd = new SqlCommand(sql, conn);                                                
                int kq = cmd.ExecuteNonQuery();
                conn.Close();
                if(kq >= 0)
                    return true;                                                 
                return false;               
            }                        
        }
        public bool KtLoaiHang(string TenLoaiHang)
        {
            string sql = "SELECT COUNT(*) FROM LoaiHang WHERE TenLoaiHang = N'"+TenLoaiHang+"'";
            using (SqlConnection conn = DataProvider.ConnectDB())
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader read = cmd.ExecuteReader();
                read.Read();
                int kq = read.GetInt32(0);                
                conn.Close();
                if(kq == 0)
                    return true;
                return false;                
            }        
        }
        public List<LoaiHangDTO> LayLoaiHang()
        {
            string sql = "SELECT * FROM LoaiHang WHERE Deleted = 0";
            using (SqlConnection conn = DataProvider.ConnectDB())
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader read = cmd.ExecuteReader();
                List<LoaiHangDTO> listLh = new List<LoaiHangDTO>();
                while (read.Read())
                {
                    LoaiHangDTO lh = new LoaiHangDTO();
                    lh.MaLoaiHang = read.GetInt32(0);
                    lh.TenLoaiHang = read.GetString(1);

                    listLh.Add(lh);
                }
                conn.Close();
                return listLh;
            }
        }
        public LoaiHangDTO LayLoaiHangTheoMa(int MaLoaiHang)
        {
            string sql = "SELECT * FROM LoaiHang WHERE MaLoaiHang = "+MaLoaiHang;
            using (SqlConnection conn = DataProvider.ConnectDB())
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader read = cmd.ExecuteReader();
                read.Read();
                LoaiHangDTO lh = new LoaiHangDTO();
                lh.MaLoaiHang = read.GetInt32(0);
                lh.TenLoaiHang = read.GetString(1);
                conn.Close();
                return lh;
            }
        }
        public bool CapNhatLoaiHang(string TenLoaiHangMoi, string TenLoaiHangCu)
        {
            string sql = "UPDATE LoaiHang SET TenLoaiHang = N'" + TenLoaiHangMoi + "' WHERE TenLoaiHang = N'"+ TenLoaiHangCu +"' AND Delete=0";
            using (SqlConnection conn = DataProvider.ConnectDB())
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                int kq = cmd.ExecuteNonQuery();
                conn.Close();
                if (kq >= 0)
                    return true;
                return false;                
            }
        }
        public bool XoaLoaiHang(string TenLoaiHang)
        {
            string sql = "UPDATE LoaiHang SET Delete=1 WHERE TenLoaiHang = N'"+TenLoaiHang+"'";
            using (SqlConnection conn = DataProvider.ConnectDB())
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                int kq = cmd.ExecuteNonQuery();
                conn.Close();
                if (kq >= 0)
                    return true;
                return false;
            }
        }
    }
}
